﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Data.SqlClient;
using Entities;
using DataAccess;
using Common.Constants;
[assembly: CLSCompliant(true)]
namespace Business
{
    /// <summary>
    /// Contact Business
    /// </summary>
    public class ContactBusiness
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static ContactBusiness Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="ContactDataAccess" /> class.
        /// </summary>
        static ContactBusiness() { Instance = new ContactBusiness(); }

        /// <summary>
        /// Lists the contact.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="announce">The announce.</param>
        /// <returns></returns>
        public Collection<Contact> ListContact(string firstName, string surname, bool isActive, out string announce)
        {
            if (firstName == null || surname == null)
            {
                announce = UIMessage.SearchEmtpy;
                return null;
            }
            try
            {
                var result = ContactDataAccess.Instance.ListContact(firstName, surname, isActive);
                announce = "Total records: " + result.Count;
                return result;
            }
            catch (SqlException exception)
            {
                announce = exception.Message;
                return null;
            }


        }

        /// <summary>
        /// Lists the lead contact.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="announce">The announce.</param>
        /// <returns></returns>
        public Collection<Contact> ListLeadContact(string firstName, string surname, bool isActive, out string announce)
        {
            if (firstName == null || surname == null)
            {
                announce = UIMessage.SearchEmtpy;
                return null;
            }
            try
            {

                var result = ContactDataAccess.Instance.ListContact(firstName, surname, isActive);
                var query = result.Where(c => c.ManagerId == Guid.Empty || c.ManagerId == null).ToList();

                announce = "Total records: " + query.Count;
                return new Collection<Contact>(query);
            }
            catch (SqlException exception)
            {
                announce = exception.Message;
                return null;
            }


        }
        /// <summary>
        /// Checks the name of the contact.
        /// </summary>
        /// <param name="fistname">The fistname.</param>
        /// <param name="surname">The surname.</param>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public bool CheckContactName(string fistname, string surname, Guid contactId)
        {
            string announce ;
            var lstContact = ListContact("", "", false, out announce);
            var query =
                lstContact.Where(
                    c => c.FirstName.Equals(fistname) && c.SurName.Equals(surname) && c.ContactId != contactId).FirstOrDefault();
            if (query != null)
                return true;
            return false;
        }

        /// <summary>
        /// Pagings the contact.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="contacts">The contacts.</param>
        /// <returns></returns>
        public Collection<Contact> PagingContact(int pageNumber, int pageSize, Collection<Contact> contacts)
        {
            if (contacts != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = contacts.Count % pageSize == 0 ? contacts.Count / pageSize : contacts.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<Contact>(contacts.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;

        }



        /// <summary>
        /// Gets the contact.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <param name="announce">The announce.</param>
        /// <returns></returns>
        public Contact GetContact(string contactId, out string announce)
        {
            if (string.IsNullOrEmpty(contactId) == false && contactId != Guid.Empty.ToString())
            {
                try
                {
                    var contac = ContactDataAccess.Instance.GetContact(contactId);
                    if (contac != null)
                    {
                        announce = "Successfull";
                        return contac;
                    }
                    announce = "ContactId not valid";
                    return null;
                }
                catch (SqlException exception)
                {
                    announce = exception.Message;
                    return null;
                }
            }
            announce = "ContactId not valid";
            return null;
        }
        /// <summary>
        /// Gets the type of the contact.
        /// </summary>
        /// <returns></returns>
        public Collection<ReferenceData> GetContactType()
        {

            return ReferenceDataDataAccess.Instance.GetReferenceData((int)RefCode.ContactType
                                                                     );



        }

        /// <summary>
        /// Bests the contact method.
        /// </summary>
        /// <returns></returns>
        public Collection<ReferenceData> BestContactMethod()
        {
            return ReferenceDataDataAccess.Instance.GetReferenceData((int)RefCode.BestContactMethod);
        }

        /// <summary>
        /// Inserts the specified contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public string Insert(Contact contact)
        {
            if (contact != null)
            {

                try
                {
                    if (contact.ContactId != Guid.Empty && !string.IsNullOrEmpty(contact.SurName) && !string.IsNullOrEmpty(contact.FirstName))
                        return ContactDataAccess.Instance.Insert(contact) > 0 ? UIMessage.InsertContactSuccess : UIMessage.InsertContactFalse;

                    return UIMessage.InsertContactFalse;

                }
                catch (SqlException exception)
                {

                    return exception.Message;
                }

            }
            return UIMessage.FieldEmptyContact;
        }
        /// <summary>
        /// Updates the specified contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public string Update(Contact contact)
        {

            if (contact != null)
            {
                try
                {
                    if (contact.ContactId != Guid.Empty && !string.IsNullOrEmpty(contact.FirstName) && !string.IsNullOrEmpty(contact.SurName))
                        return ContactDataAccess.Instance.Update(contact) > 0 ? UIMessage.UpdateContactSuccess : UIMessage.UpdateContactFalse;
                    return UIMessage.UpdateContactFalse;
                }
                catch (SqlException exception)
                {

                    return exception.Message;
                }
            }
            return UIMessage.FieldEmptyContact;
        }
        /// <summary>
        /// Deletes the specified contact id.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public bool Delete(string contactId)
        {
            if (!string.IsNullOrEmpty(contactId))
            {

                return ContactDataAccess.Instance.Delete(contactId) > 0;

            }
            return false;

        }

    }
}
